package com.xxwy.dependency.injection.qualifier;

import com.xxwy.dependency.User;
import com.xxwy.dependency.injection.qualifier.annotation.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;

/**
 * @author wy
 * @date 2020/4/27 14:55
 */
@Configuration
public class QualiferAnnotationDependenctInjectionDemo {

  @Autowired
  private User user3;

  @Autowired
  @Qualifier("use1") //制定Bean名称或ID
  private User mUser;

  @Autowired
  private Collection<User> users;

  @Bean
  @Qualifier //进行逻辑分组
  public User use1(){
    return new User(7);
  }
  @Bean
  @Qualifier //进行逻辑分组
  public User use2(){
    return new User(8);
  }

  @Autowired
  @Qualifier
  private Collection<User> qualifiedUsers; // user1 + user2 + user3 + user4 因为@UserGroup是@Qualifier派生

  @Bean
  @UserGroup
  public User user3(){
    return new User(10);
  }

  @Bean
  @UserGroup
  public User user4(){
    return new User(11);
  }

  @Autowired
  @UserGroup
  private Collection<User> userGroups; // user3 + user4



  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(QualiferAnnotationDependenctInjectionDemo.class);

    //XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(context);
    //int definitions = definitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");

    context.refresh();

    QualiferAnnotationDependenctInjectionDemo bean = context.getBean(QualiferAnnotationDependenctInjectionDemo.class);
    //System.out.println(bean.mUser);
    //System.out.println(bean.user);
    System.out.println("qualified" + bean.qualifiedUsers);
    System.out.println("allUser" + bean.users);
    System.out.println("userGroup" + bean.userGroups);

  }
}
